package me.ywy.apt.mediaplayer;

import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by ywy on 2018-04-13.
 */
public class DNPlayer implements SurfaceHolder.Callback {

    static{
        System.loadLibrary("media_lib");
    }

    private SurfaceView surfaceView;
    //设置播放文件
    private native void native_play(String path);
    //设置画布
    private native void native_set_display(Surface surface);
    //停止
    private native void native_stop();
    //释放
    private native void native_release();


    public void setDisplay(SurfaceView surfaceView){
        if(this.surfaceView != null){
            this.surfaceView.getHolder().removeCallback(this);
        }

        this.surfaceView = surfaceView;
        native_set_display(surfaceView.getHolder().getSurface());
        this.surfaceView.getHolder().addCallback(this);
    }

    public void stop(){
        native_stop();
    }

    public void play(String path){
        if(path == null){
            return;
        }

        native_play(path);

    }

    public void release() {
        native_release();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        native_set_display(holder.getSurface());
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}
